package user.zc;

import user.zc.action.*;
import user.zc.controller.ISystemConfig;
import user.zc.controller.ISystemTemplate;
import user.zc.controller.impl.GuiProxyFactory;
import user.zc.controller.IConnect;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;

public class Start extends JFrame implements ActionListener {
    private JTextArea textArea;
    private IConnect connectCollector = GuiProxyFactory.getConnectionProxy();
    private ISystemConfig systemConfigController = GuiProxyFactory.getStemConfigProxy();
    private ISystemTemplate templateConfigController = GuiProxyFactory.getSysteTemplateConfigProxy();

    JMenuItem openItem;
    JMenuItem managerItem;
    JMenuItem managerDefaultColumnItem ;
    JMenuItem managerTemplateItem;
    private  Start(){
        setTitle("代码生成器");
        setSize(800, 600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null); // 将弹出窗口居中显示
        textArea = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea);
        add(scrollPane, BorderLayout.CENTER);
        JMenuBar menuBar = new JMenuBar();
//=======================数据库==============================

        JMenu dbMenu = new JMenu("数据库");

        openItem = new JMenuItem("连接");
        openItem.addActionListener(this);
        dbMenu.add(openItem);

        JMenuItem addItem = new JMenuItem("新建");
        addItem.addActionListener(this);
        dbMenu.add(addItem);

        managerItem = new JMenuItem("管理");
        managerItem.addActionListener(this);
        dbMenu.add(managerItem);
        menuBar.add(dbMenu);


//=======================系统配置============================
        JMenu systemConfigMenu = new JMenu("系统配置");

        JMenuItem addDefaultColumnItem = new JMenuItem("系统默认列-新建");
        addDefaultColumnItem.addActionListener(this);
        systemConfigMenu.add(addDefaultColumnItem);

        managerDefaultColumnItem = new JMenuItem("系统默认列-管理");
        managerDefaultColumnItem.addActionListener(this);
        systemConfigMenu.add(managerDefaultColumnItem);

        systemConfigMenu.addSeparator();

        JMenuItem addTemplateItem = new JMenuItem("系统模板-新建");
        addTemplateItem.addActionListener(this);
        systemConfigMenu.add(addTemplateItem);

        managerTemplateItem = new JMenuItem("系统模板-管理");
        managerTemplateItem.addActionListener(this);
        systemConfigMenu.add(managerTemplateItem);


        menuBar.add(systemConfigMenu);

//=====================================================
        setJMenuBar(menuBar);
        setVisible(true);

    }

    public static void main(String[] args) {
        new Start();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand().equals("连接")) {
            new SelectConnectionAction(connectCollector,(value)->{
                //成功了，需要展示用户信息
                setTitle("代码生成器-当前连接数据库["+value+"]");
            });
        } else if (e.getActionCommand().equals("新建")) {
            new AddOrUpdateConnectionAction(connectCollector,(value)->{
                new java.util.Timer().schedule(new TimerTask() {
                    public void run() {
                        managerItem.doClick();
                    };
                },100);

            });
        } else if (e.getActionCommand().equals("管理")) {
            ManagerConnectionAction popupWindow = new ManagerConnectionAction(connectCollector);
            popupWindow.setVisible(true);
        } else if (e.getActionCommand().equals("系统默认列-新建")) {
            new AddOrUpdateSystemDefaultColumnAction(systemConfigController,(value)->{
                new java.util.Timer().schedule(new TimerTask() {
                    public void run() {
                        managerDefaultColumnItem.doClick();
                    };
                },100);


            });
        } else if (e.getActionCommand().equals("系统默认列-管理")) {
            ManagerSystemDefaultColumnAction popupWindow = new ManagerSystemDefaultColumnAction(systemConfigController);
            popupWindow.setVisible(true);
        } else if (e.getActionCommand().equals("系统模板-管理")) {
            ManagerSystemTemplateAction popupWindow = new ManagerSystemTemplateAction(templateConfigController);
            popupWindow.setVisible(true);
        } else if (e.getActionCommand().equals("系统模板-新建")) {
            new AddOrUpdateSystemTemplateAction(templateConfigController,(value)->{
                new java.util.Timer().schedule(new TimerTask() {
                    public void run() {
                        managerTemplateItem.doClick();
                    };
                },100);
            });


        }
    }
}
